<?php

/**
 * 采购单管理
 */

namespace app\admin\controller;

use think\Request;
use app\admin\service\Purchase as PurchaseService;
use app\admin\validate\Purchase as PurchaseValidate;
use app\common\Common;

class Purchase extends BaseController
{
    /**
     * 获取采购单列表
     * @param Request $request
     * @param PurchaseService $purchaseService
     * @param PurchaseValidate $validate
     * @return \think\response\Json
     */
    public function getList(Request $request, PurchaseService $purchaseService, PurchaseValidate $validate, Common $oCommon)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['name'] =  $request->param('name', null);
        $param['start_date'] =  $request->param('start_date', null);
        $param['end_date'] =  $request->param('end_date', null);
        $param['purchase_no'] =  $request->param('purchase_no', null);
        $param['pay_status'] =  $request->param('pay_status', null);
        $param['status'] =  $request->param('status', null);
        $param['owner'] =  $request->param('owner', null);
        $param['supid'] =  $request->param('supid', null);
        $param['is_futures'] =  $request->param('is_futures', null);
        $param['type'] =  $request->param('type', null);
        $param['contract_no'] =  $request->param('contract_no', null);
        $param['product_id'] =  $request->param('product_id', null);

        $intPage =  $request->param('pageIndex', 1);
        $intPageSize =  $request->param('pageSize', 50);
        $arrData = $purchaseService->getList($param, (int)$intPage, (int)$intPageSize);
        return json($arrData);
    }

    /**
     * 采购单添加和修改
     * @param Request $request
     * @param PurchaseService $purchaseService
     * @param PurchaseValidate $validate
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request, PurchaseService $purchaseService, PurchaseValidate $validate)
    {
        $checkData = $request->param();

        $checkType = isset($checkData['id']) ? 'save' : 'create';
        if (!$validate->scene($checkType)->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;

        $param['advance_delivery_date'] =  $request->param('advance_delivery_date', null);
        $param['notes'] =  $request->param('notes', null);
        $param['other_amount'] =  $request->param('other_amount', null);
        $param['owner'] =  $request->param('owner', null);
        $param['partner'] =  $request->param('partner', []);
        $param['pay_amount'] =  $request->param('pay_amount', null);
        $param['pay_terms'] =  $request->param('pay_terms', null);
        $param['pay_type'] =  $request->param('pay_type', null);
        $param['purchase_no'] =  $request->param('purchase_no', null);
        $param['purchase_goods'] =  $request->param('purchase_goods', []); //采购商品数组
        $param['sales_orderno'] =  $request->param('sales_orderno', null);
        $param['order_id'] =  $request->param('order_id', null);
        $param['signatoryid'] =  $request->param('signatoryid', null);
        $param['supid'] =  $request->param('supid', null);
        $param['total_amount'] =  $request->param('total_amount', null);
        $param['currency'] =  $request->param('currency', null);
        $param['product_total_amount'] =  $request->param('product_total_amount', null);
        $param['type'] =  $request->param('type', null);
        $param['contract_no'] =  $request->param('contract_no', null);

        $param['id'] =  $request->param('id', null);

        $arrData = $purchaseService->save($param);
        return json($arrData);
    }

    /**
     * 采购单详情
     * @param Request $request 采购单ID
     * @return \think\response\Json 返回数据
     * @author huangfeng
     * @date 2024-6-28
     */
    public function getInfo(Request $request, PurchaseService $purchaseService, PurchaseValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('show')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id', null);
        $arrData = $purchaseService->getInfo($param);
        return json($arrData);
    }

    /**
     * 删除单条或多条数据
     * @param Request $request
     * @param PurchaseValidate $validate
     * @param PurchaseService $purchaseService
     * @return \think\response\Json
     */
    public function delete(Request $request, PurchaseValidate $validate, PurchaseService $purchaseService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids', null);
        $arrData = $purchaseService->delete($param);
        return json($arrData);
    }

    public function getFieldEnum(Request $request, PurchaseService $purchaseService)
    {
        $param = $request->param('field', null);
        $arrData = $purchaseService->getFieldEnum($param);
        return json($arrData);
    }

    /**
     * 采购单审核
     * @param Request $request
     * @param PurchaseService $purchaseService
     * @param PurchaseValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function examine(Request $request, PurchaseService $purchaseService, PurchaseValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('examine')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id', null);
        $param['info'] = $request->param('info', null);
        $arrData = $purchaseService->examine($param);
        return json($arrData);
    }

    public function confirm(Request $request, PurchaseService $purchaseService, PurchaseValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('confirm')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['info'] = $request->param('info', '');
        $arrData = $purchaseService->confirm($param);
        return json($arrData);
    }

    /**
     * 采购商品入库操作
     * @param Request $request
     * @param PurchaseService $purchaseService
     * @param PurchaseValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function entry(Request $request, PurchaseService $purchaseService, PurchaseValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('entry')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['entry_num'] = $request->param('entry_num', null);
        $param['warehouse'] = $request->param('warehouse', null);
        $param['futures'] = $request->param('futures', null);
        $param['delivery_date'] = $request->param('delivery_date', null);
        $param['remark'] = $request->param('remark', '');
        $param['arrival_batch'] = $request->param('arrival_batch', '');
        $param['ccname_ids'] = $request->param('ccname_ids', null);
        $arrData = $purchaseService->entry($param);
        return json($arrData);
    }

    /**
     * 批量入库
     * @param Request $request
     * @param PurchaseService $purchaseService
     * @param PurchaseValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function batchEntry(Request $request, PurchaseService $purchaseService, PurchaseValidate $validate)
    {
        $checkData = $request->param();

        if (!$validate->scene('entry')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['warehouse'] = $request->param('warehouse', null);
        $param['remark'] = $request->param('remark', '');
        $param['futures'] = $request->param('futures', null);
        $param['arrival_batch'] = $request->param('arrival_batch', null);
        $param['delivery_date'] = $request->param('delivery_date', null);
        $param['ccname_ids'] = $request->param('ccname_ids', null);
        $arrData = $purchaseService->batchEntry($param);
        return json($arrData);
    }

    /**
     *  获取销售订单号
     */
    public function salesOrderNo(Request $request, PurchaseService $purchaseService, PurchaseValidate $validate)
    {

        $checkData = $request->param();
        if (!$validate->scene('salesOrderNo')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['operator_id'] = $request->operator_id;
        $param['order_no'] = $request->param('order_no', null);

        $arrData = $purchaseService->salesOrderNo($param);
        return json($arrData);
    }


    /**
     *  获取订单商品
     */
    public function getOrderProduct(Request $request, PurchaseService $purchaseService, PurchaseValidate $validate)
    {

        // $checkData = $request->param();
        // if (!$validate->scene('entry')->check($checkData)) {
        //     return json(['code' => 1, 'message' => $validate->getError()]);
        // }

        $param['operator_id'] = $request->operator_id;
        $param['order_id'] = $request->param('order_id', null);

        $arrData = $purchaseService->getOrderProduct($param);
        return json($arrData);
    }

    /**
     * 红冲
     * @param Request $request
     * @param PurchaseService $purchaseService
     * @param PurchaseValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function redInkReversal(Request $request, PurchaseService $purchaseService, PurchaseValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('redInkReversal')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['type'] = $request->param('type', null);
        $param['payments_id'] = $request->param('payments_id', null);
        $param['amount'] = $request->param('amount', null);
        $param['number'] = $request->param('number', null);
        $param['purchase_receipt_details_id'] = $request->param('purchase_receipt_details_id', null);
        $arrData = $purchaseService->redInkReversal($param);
        return json($arrData);
    }
}
